package com.jh.fcsm.controller.inf;

import com.github.pagehelper.PageInfo;
import com.jh.fcsm.beans.inf.InfSqlCode;
import com.jh.fcsm.beans.inf.vo.InfSqlCodeVo;
import com.jh.fcsm.common.BaseController;
import com.jh.fcsm.common.RestApiResponse;
import com.jh.fcsm.common.annotation.RepeatSubAnnotation;
import com.jh.fcsm.common.annotation.SystemLogAnnotation;
import com.jh.fcsm.common.redis.RedisUtil;
import com.jh.fcsm.constant.CommonConstant;
import com.jh.fcsm.service.inf.InfSqlCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * API接口配置Controller
 * 
 * @author linqiang
 * @date 2022-08-01
 */
@RestController
@RequestMapping("/inf/code")
@Api(value="API接口配置", tags ="API接口配置")
public class InfSqlCodeController extends BaseController {
    @Autowired
    private InfSqlCodeService infSqlCodeService;
	@Autowired
	private RedisUtil redisUtil;
	private static final String PER_PREFIX = "btn:inf:code:";
	
	/**
	 *@Description: 新增API接口配置
	 *@param infSqlCode API接口配置数据 json
	 *@return RestApiResponse<?>
	 *@Author: linqiang
	 */
	@RepeatSubAnnotation
	@PostMapping("/save")
	@ApiOperation(value = "新增API接口配置", notes = "新增API接口配置")
	@SystemLogAnnotation(type = "API接口配置",value = "新增API接口配置")
	@PreAuthorize("hasAuthority('"+PER_PREFIX+"save')")
	public RestApiResponse<?> saveInfSqlCode(@RequestBody InfSqlCode infSqlCode) {
		String id = infSqlCodeService.saveOrUpdateInfSqlCode(infSqlCode);
		return RestApiResponse.ok(id);
	}
	
	/**
	 *@Description: 修改API接口配置
	 *@param infSqlCode API接口配置数据 json
	 *@return RestApiResponse<?>
	 *@Author: linqiang
	 */
	@RepeatSubAnnotation
	@PostMapping("/update")
	@ApiOperation(value = "修改API接口配置", notes = "修改API接口配置")
	@SystemLogAnnotation(type = "API接口配置",value = "修改API接口配置")
	@PreAuthorize("hasAuthority('"+PER_PREFIX+"update')")
	public RestApiResponse<?> updateInfSqlCode(@RequestBody InfSqlCode infSqlCode) {
		String id = infSqlCodeService.saveOrUpdateInfSqlCode(infSqlCode);
		return RestApiResponse.ok(id);
	}
	
	/**
	 *@Description: 批量删除API接口配置(判断 关联数据是否可以删除)
	 *@param ids
	 *@return RestApiResponse<?>
	 *@Author: linqiang
	 */
	@RepeatSubAnnotation
	@PostMapping("/delete")
	@ApiOperation(value = "批量删除API接口配置", notes = "批量删除API接口配置")
	@SystemLogAnnotation(type = "API接口配置",value = "批量删除API接口配置")
	@PreAuthorize("hasAuthority('"+PER_PREFIX+"delete')")
	public RestApiResponse<?> deleteInfSqlCode(@RequestBody List<String> ids) {
		//判断 关联数据是否可以删除
		infSqlCodeService.deleteInfSqlCode(ids);
		return RestApiResponse.ok();
	}
	/**
	 *@Description: 清除缓存
	 *@param code
	 *@return RestApiResponse<?>
	 *@Author: linqiang
	 */
	@RepeatSubAnnotation
	@PostMapping("/clearCache/{code}")
	@ApiOperation(value = "清除缓存", notes = "清除缓存")
	@SystemLogAnnotation(type = "清除缓存",value = "清除缓存")
	@PreAuthorize("hasAuthority('"+PER_PREFIX+"delete')")
	public RestApiResponse<?> clear(@PathVariable("code") String code){
		try {
			redisUtil.delMenu(CommonConstant.SYS_REDIS + "dp:" + code);
			logger.info("{}接口缓存清除成功",code);
		}catch (Exception e){
			logger.error("{}清除出错，{}",code,e);
			return RestApiResponse.error("缓存清除失败！");
		}
		return RestApiResponse.ok();
	}
	/**
	 *@Description: 查询API接口配置详情
	 *@param id
	 *@return RestApiResponse<?>
	 *@Author: linqiang
	 */
	@GetMapping("/findById")
	@ApiOperation(value = "查询API接口配置详情", notes = "查询API接口配置详情")
	@PreAuthorize("hasAuthority('"+PER_PREFIX+"find')")
	public RestApiResponse<?> findById(@RequestParam("id") String id) {
		InfSqlCode  infSqlCode=infSqlCodeService.findById(id);
		return RestApiResponse.ok(infSqlCode);
	}
	
	/**
	 *@Description: 分页查询API接口配置
	 *@param infSqlCodeVo API接口配置 查询条件
	 *@return RestApiResponse<?>
	 *@Author: linqiang
	 */
	@PostMapping("/findPageByQuery")
	@ApiOperation(value = "分页查询API接口配置", notes = "分页查询API接口配置")
	@PreAuthorize("hasAuthority('"+PER_PREFIX+"query')")
	public RestApiResponse<?> findPageByQuery(@RequestBody InfSqlCodeVo infSqlCodeVo) {
		PageInfo<InfSqlCode>  infSqlCode=infSqlCodeService.findPageByQuery(infSqlCodeVo);
		return RestApiResponse.ok(infSqlCode);
	}
	
}
